package book;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    private static List<Book> LIST = new ArrayList<>();

    public static void main(String[] args) {
        readDate();
        Scanner scan = new Scanner(System.in);
        while (true){
            System.out.println("####################");
            System.out.println("#图书管理系统        #");
            System.out.println("#1添加图书          #");
            System.out.println("#2修改图书          #");
            System.out.println("#3查询图书列表       #");
            System.out.println("#4删除图书          #");
            System.out.println("#（按任意键退出系统） #");
            System.out.println("####################");
            String flag = scan.nextLine();
            switch (flag){
                case "1":
                    System.out.println("==添加图书==");
                    insertBook(scan);
                    break;
                case "2":
                    System.out.println("==修改图书==");
                    modifyBook(scan);
                    break;
                case "3":
                    System.out.println("==查询图书列表==");
                    showBook();
                    break;
                case "4":
                    System.out.println("==删除图书==");
                    deleteBook(scan);
                    break;
                default:
                    saveDate();
                    System.out.println("==退出系统==");
                    return;
            }
        }
    }

    private static class Book implements Serializable{
        private String name;
        private String author;
        private double price;

        public Book name(String name) {
            this.name = name;
            return this;
        }

        public Book author(String author) {
            this.author = author;
            return this;
        }

        public Book price(double price) {
            this.price = price;
            return this;
        }

        @Override
        public String toString() {
            return "书籍{" +
                    "书名='" + name + '\'' +
                    ", 作者='" + author + '\'' +
                    ", 价格=" + price +
                    '}';
        }
    }

    @SuppressWarnings("unchecked")
    private static void readDate(){
        File file = new File("data");
        if (file.exists()){
            try(ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("data"))){
                LIST = (List<Book>) inputStream.readObject();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }else {
            LIST = new ArrayList<>();
        }

    }

    private static void saveDate(){
        try(ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("data"))) {
            outputStream.writeObject(LIST);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void insertBook(Scanner scanner){
        LIST.add(new Book()
                .name(scanner.nextLine())
                .author(scanner.nextLine())
                .price(scanner.nextDouble()));
        scanner.nextLine();
    }

    private static void modifyBook(Scanner scanner){
        int i = 1;
        for (Book book:LIST) System.out.println((i++)+"."+book);
        int index = scanner.nextInt();
        scanner.nextLine();
        if (index > LIST.size()&&index<=0) System.out.println("错误的序号");
        else LIST
                .get(index-1)
                .name(scanner.nextLine())
                .author(scanner.nextLine())
                .price(scanner.nextDouble());
        scanner.nextLine();
    }

    private static void showBook(){
        /**
         * 1这个表达式不同
         */
        LIST.forEach(i -> System.out.println(i));
        //        LIST.forEach(System.out::println);
    }

    private static void deleteBook(Scanner scanner){
        int i = 1;
        for (Book book:LIST) System.out.println((i++)+"."+book);
        int index = scanner.nextInt();
        if (index > LIST.size()&&index<=0) System.out.println("错误的序号");
        else LIST.remove(index-1);
        scanner.nextLine();
    }
}
